/**
 * Date: Apr 21, 2008
 */
package forteresce.portprofile.gui.dialogs;

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Component;
import java.awt.Dimension;
import java.awt.Font;
import java.awt.Frame;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Image;
import java.awt.Insets;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.IOException;
import java.net.URL;

import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JDialog;
import javax.swing.JEditorPane;
import javax.swing.JFileChooser;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.SpringLayout;
import javax.swing.event.HyperlinkEvent;
import javax.swing.event.HyperlinkListener;
import javax.swing.filechooser.FileFilter;

import org.apache.log4j.Logger;

/**
 * this class provides static methods to generate various
 * dialog boxes for the application
 * @author forteresce
 *
 */
public class PortProfileDialogs extends JDialog implements ActionListener, HyperlinkListener{
	private static final long serialVersionUID = 1L;

	//dialog titles and other strings
	final static String error = "Error";
	final static String about = "About Portprofile";
	final static String confirm = "Confirm";
	final static String info = "Info";
	final static String ok = "OK";
	final static String howto = "Portprofile: How to?";
	
	private static Logger log = Logger.getLogger(PortProfileDialogs.class);

	JButton okButton;

	/**
	 * private constructor
	 * @param frame
	 * @param title
	 */
	private PortProfileDialogs(Frame frame, String title) {
		super(frame, title, true);
		setLocationRelativeTo(frame);
		setLayout(new BorderLayout());
		setResizable(false);
	}
	
	/**
	 * simple show error method which directly calls on the JOptionPane
	 * @param c
	 * @param msg
	 */
	public static void showError(Component c, String msg) {
		JOptionPane.showMessageDialog(c, msg, error, JOptionPane.ERROR_MESSAGE);
	}
	
	public static String showPrompt(Component c, String msg) {
		return JOptionPane.showInputDialog(c, msg);
	}

	/**
	 * simple show confirm method which directly calls on the JOptionPane
	 * @param c
	 * @param msg
	 * @return
	 */
	public static int showConfirm(Component c, String msg) {
		return JOptionPane.showConfirmDialog(c, msg, confirm, JOptionPane.YES_NO_OPTION);
	}
	
	/**
	 * simple show info method which directly calls on the JOptionPane
	 * @param c
	 * @param msg
	 */
	public static void showInfo(Component c, String msg) {
		JOptionPane.showMessageDialog(c, msg, info, JOptionPane.INFORMATION_MESSAGE);
	}
	
	/**
	 * the about dialog that shows a image with the details
	 * @param frame
	 */
	public static void showAbout(Frame frame) {
		final PortProfileDialogs aboutDialog = new PortProfileDialogs(frame, about);
		aboutDialog.setBackground(Color.WHITE);
		aboutDialog.setForeground(Color.WHITE);
		
		//set dimensions
		Dimension dim = new Dimension(350, 225);
		aboutDialog.setPreferredSize(dim);
		aboutDialog.setMinimumSize(dim);
		aboutDialog.setMaximumSize(dim);

		//get the image file
		ImageIcon portProfileIcon = new ImageIcon(PortProfileDialogs.class.getResource("/portprofile_icon.png"));
		Image portProfileImage = portProfileIcon.getImage().getScaledInstance(portProfileIcon.getIconWidth()/2, portProfileIcon.getIconHeight()/2, Image.SCALE_SMOOTH);
		JLabel iconLabel = new JLabel(new ImageIcon(portProfileImage));
		
		//about panel
		JPanel aboutPanel = new JPanel();
		SpringLayout aboutPanelLayout = new SpringLayout();
		aboutPanel.setLayout(aboutPanelLayout);
		aboutPanel.setBackground(Color.WHITE);
		
		JLabel appNameLabel = new JLabel("Portprofile");
		appNameLabel.setFont(new Font(Font.SANS_SERIF, Font.BOLD, 20));
		appNameLabel.setForeground(Color.RED);
		
		String version = (null != PortProfileDialogs.class.getPackage().getImplementationVersion()) ?
						PortProfileDialogs.class.getPackage().getImplementationVersion() : "1.0t";
		JLabel versionLabel = new JLabel();
		versionLabel.setFont(new Font(Font.MONOSPACED, Font.BOLD, 12));
		versionLabel.setForeground(Color.LIGHT_GRAY);
		versionLabel.setText("version " + version);
		
		
		aboutPanelLayout.putConstraint(SpringLayout.WEST, appNameLabel, 15, SpringLayout.WEST, aboutPanel);
		aboutPanelLayout.putConstraint(SpringLayout.NORTH, appNameLabel, 25, SpringLayout.NORTH, aboutPanel);
		aboutPanel.add(appNameLabel);

		aboutPanelLayout.putConstraint(SpringLayout.WEST, versionLabel, 15, SpringLayout.WEST, aboutPanel);
		aboutPanelLayout.putConstraint(SpringLayout.NORTH, versionLabel, 25, SpringLayout.NORTH, appNameLabel);
		aboutPanel.add(versionLabel);
		
		JButton okButton = new JButton(ok);
		okButton.setBackground(Color.WHITE);
		okButton.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
				aboutDialog.dispose();
			}
		});
		
		aboutPanelLayout.putConstraint(SpringLayout.WEST, okButton, 40, SpringLayout.WEST, aboutPanel);
		aboutPanelLayout.putConstraint(SpringLayout.NORTH, okButton, 50, SpringLayout.NORTH, versionLabel);
		aboutPanel.add(okButton);

		//add to the dialog
		aboutDialog.setLayout(new GridBagLayout());
		GridBagConstraints constraints = new GridBagConstraints();
		
		//first the about panel
		constraints.gridx = 0;
		constraints.gridy = 0;
		constraints.weightx = 0.5;
		constraints.fill = GridBagConstraints.BOTH;
		aboutDialog.add(aboutPanel, constraints);
		
		//the icon
		constraints.gridx = 1;
		constraints.gridy = 0;
		constraints.weightx = 0.5;
		aboutDialog.add(iconLabel);
		
		//pack and show
		aboutDialog.pack();
		aboutDialog.setVisible(true);
	}
	
	/**
	 * the howto dialog uses a JEditorPane to show the text from
	 * a html file
	 * @param frame
	 */
	public static void showHowto(Frame frame) {
		final PortProfileDialogs howtoDialog = new PortProfileDialogs(frame, howto);
		howtoDialog.setLayout(new BorderLayout());
		
		JEditorPane howtoPane = new JEditorPane();
        howtoPane.setEditable(false);
        howtoPane.setContentType("text/html");
        howtoPane.setMinimumSize(new Dimension(200, 300));
        howtoPane.addHyperlinkListener(howtoDialog);
        
        URL helpURL = PortProfileDialogs.class.getResource("/howto.html");
		
        if (helpURL != null) {
            try {
                howtoPane.setPage(helpURL);
            } catch (IOException e) {
                System.err.println("Attempted to read a bad URL: " + helpURL);
            }
        } else {
            log.error("Couldn't find file.");
        }
        
		JButton okButton = new JButton(ok);
		okButton.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
				howtoDialog.dispose();
			}
		});

		howtoDialog.setLayout(new GridBagLayout());
		GridBagConstraints constraints = new GridBagConstraints();
		
		//first the about panel
		constraints.gridx = 0;
		constraints.gridy = 0;
		constraints.weightx = 0.5;
		constraints.weighty = 0.95;
		constraints.fill = GridBagConstraints.BOTH;
		howtoDialog.add(new JScrollPane(howtoPane), constraints);
		
		//the ok button
		constraints.insets = new Insets(10, 5, 5, 5);
		constraints.gridx = 0;
		constraints.gridy = 1;
		constraints.weightx = 0.5;
		constraints.weighty = 0.05;
		constraints.fill = GridBagConstraints.VERTICAL;
		howtoDialog.add(okButton, constraints);

        
        //set dimensions
		Dimension size = new Dimension(300, 275);
		howtoDialog.setPreferredSize(size);
		howtoDialog.setMinimumSize(size);
		howtoDialog.setMaximumSize(size);
		howtoDialog.pack();
		howtoDialog.setVisible(true);
	}
	
	/**
	 * @return selected file from the file chooser or null
	 */
	public static String showFileChooser(Component parent, FileFilter filter) {
		JFileChooser fileChooser = new JFileChooser();
		fileChooser.setAcceptAllFileFilterUsed(false);
		fileChooser.setFileFilter(filter);
		
		int returnVal = fileChooser.showOpenDialog(parent);
	    if (returnVal == JFileChooser.APPROVE_OPTION) {
	       return fileChooser.getSelectedFile().getAbsolutePath();
	    }

		return null;
	}
	
	/* (non-Javadoc)
	 * @see java.awt.event.ActionListener#actionPerformed(java.awt.event.ActionEvent)
	 */
	public void actionPerformed(ActionEvent e) {
		dispose();
	}
	
	/* (non-Javadoc)
	 * @see javax.swing.event.HyperlinkListener#hyperlinkUpdate(javax.swing.event.HyperlinkEvent)
	 */
	public void hyperlinkUpdate(HyperlinkEvent e) {
		if(e.getEventType() == HyperlinkEvent.EventType.ACTIVATED) {
			JEditorPane pane = (JEditorPane) e.getSource();
			try {
				pane.setPage(e.getURL());
			} catch (IOException e1) {
				System.out.println("Cannot update url.");
			}
		}
	}
}
